package db

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

var (
	db  *sql.DB
	err error
)

func init() {
	db, err = sql.Open("mysql", "krlin:com.kylin@tcp(101.43.181.200:3306)/gotest")
}

func ExecuteQuery() (res []string) {
	res = make([]string, 1)
	if err != nil {
		_ = fmt.Errorf("数据库连接失败！")
		return
	}
	sql := "select log from logger"
	rows, er := db.Query(sql)
	if er != nil {
		_ = fmt.Errorf("查询失败")
		return
	}
	log := ""
	fmt.Println(rows)
	for rows.Next() {
		rows.Scan(&log)
		res = append(res, log)
	}
	return
}

func SaveLog(log string) {
	sql := "insert into logger (log) value (?)"
	_, err = db.Exec(sql, log)
	if err != nil {
		fmt.Errorf("插入数据库失败！")
		return
	}
}
